package cn.cttic.order.controller;

import java.util.List;

import cn.cttic.base.api.domain.vo.BaseRecordVo;
import cn.cttic.order.api.domain.bo.*;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.cttic.common.idempotent.annotation.RepeatSubmit;
import cn.cttic.common.log.annotation.Log;
import cn.cttic.common.web.core.BaseController;
import cn.cttic.common.mybatis.core.page.PageQuery;
import cn.cttic.common.core.domain.R;
import cn.cttic.common.core.validate.AddGroup;
import cn.cttic.common.core.validate.EditGroup;
import cn.cttic.common.log.enums.BusinessType;
import cn.cttic.common.excel.utils.ExcelUtil;
import cn.cttic.order.api.domain.vo.BaseReconciliationVo;
import cn.cttic.order.service.IBaseReconciliationService;
import cn.cttic.common.mybatis.core.page.TableDataInfo;

/**
 * 对账管理
 * 前端访问路由地址为:/order/reconciliation
 *
 * @author Times.Studio
 * @date 2025-01-02
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/reconciliation")
public class BaseReconciliationController extends BaseController {

    private final IBaseReconciliationService baseReconciliationService;

    /**
     * 查询对账管理列表
     */
    @SaCheckPermission("order:reconciliation:list")
    @GetMapping("/list")
    public TableDataInfo<BaseReconciliationVo> list(BaseReconciliationBo bo, PageQuery pageQuery) {
        return baseReconciliationService.queryPageList(bo, pageQuery);
    }


    /**
     * 查询对账管理列表
     */
    @SaCheckPermission("order:reconciliation:list")
    @GetMapping("/getReconciliationList")
    public TableDataInfo<BaseReconciliationVo> getReconciliationList(BaseReconciliationQueryBo bo) {
        return baseReconciliationService.queryReconciliationPageList(bo);
    }

    /**
     * 查询对账管理列表
     */
    @SaCheckPermission("order:reconciliation:list")
    @GetMapping("/getWriteOffList")
    public TableDataInfo<BaseReconciliationVo> getWriteOffList(BaseReconciliationQueryBo bo) {
        return baseReconciliationService.queryWriteOffPageList(bo);
    }

    /**
     * 查询对账管理列表
     */
    @SaCheckPermission("order:reconciliation:list")
    @GetMapping("/getRecordList")
    public List<BaseRecordVo> getRecordList(BaseReconciliationQueryBo bo) {
        return baseReconciliationService.queryRecordList(bo);
    }

    /**
     * 确认对账单
     *
     * @param bo 确认参数
     * @return
     */
    @PostMapping(value = "/confirm")
    public R<?> confirm(@RequestBody @Valid ReconciliationConfirmBo bo) {
        try {
            BaseReconciliationVo vo = baseReconciliationService.confirm(bo.getId());
            return R.ok(vo);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail("确认对账单失败");
        }
    }

    /**
     * 确认对账单
     *
     * @param bo 确认参数
     * @return
     */
    @PostMapping(value = "/batchConfirm")
    public R<?> batchConfirm(@RequestBody @Valid ReconciliationBatchConfirmBo bo) {
        try {
            List<BaseReconciliationVo> voList = baseReconciliationService.batchConfirm(bo.getIds());
            return R.ok(voList);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail("确认对账单失败");
        }
    }


    /**
     * 审核对账单
     *
     * @param bo 确认参数
     * @return
     */
    @PostMapping(value = "/audit")
    public R<?> audit(@RequestBody @Valid ReconciliationAuditBo bo) {
        try {
            BaseReconciliationVo vo = baseReconciliationService.audit(
                bo.getId(),
                bo.getReconciliationStatus(),
                bo.getReconciliationReason(),
                bo.getWriteOffApprovalNo(),
                bo.getWriteOffProof());
            return R.ok(vo);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail("确认对账单失败");
        }
    }

    /**
     * 对账单开票
     *
     * @param bo 确认参数
     * @return
     */
    @PostMapping(value = "/ticket")
    public R<?> ticket(@RequestBody @Valid ReconciliationTicketBo bo) {
        try {
            BaseReconciliationVo vo = baseReconciliationService.ticket(bo.getId(), bo.getReceiptInfo());
            return R.ok(vo);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail("确认对账单失败");
        }
    }

    /**
     * 对账单完成
     *
     * @param bo 确认参数
     * @return
     */
    @PostMapping(value = "/accomplish")
    public R<?> accomplish(@RequestBody @Valid ReconciliationAccomplishBo bo) {
        try {
            BaseReconciliationVo vo = baseReconciliationService.accomplish(bo.getId(), bo.getPaidProof());
            return R.ok(vo);
        } catch (Exception ex) {
            ex.printStackTrace();
            return R.fail("确认对账单失败");
        }
    }


    /**
     * 导出对账管理列表
     */
    @SaCheckPermission("order:reconciliation:export")
    @Log(title = "对账管理", businessType = BusinessType.EXPORT)
    @PostMapping("/downloadReconciliationExcel")
    public void downloadReconciliationExcel(BaseReconciliationBo bo, HttpServletResponse response) {
        baseReconciliationService.exportReconciliationExcel(bo.getId(), response);
    }


    /**
     * 导出对账管理列表
     */
    @SaCheckPermission("order:reconciliation:export")
    @Log(title = "对账管理", businessType = BusinessType.EXPORT)
    @PostMapping("/downloadReconciliationPdf")
    public void downloadReconciliationPdf(BaseReconciliationBo bo, HttpServletResponse response) {
        baseReconciliationService.exportReconciliationPdf(bo.getId(), response);
    }


    /**
     * 导出对账管理列表
     */
    @SaCheckPermission("order:reconciliation:export")
    @Log(title = "对账管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(BaseReconciliationBo bo, HttpServletResponse response) {
        List<BaseReconciliationVo> list = baseReconciliationService.queryList(bo);
        ExcelUtil.exportExcel(list, "对账管理", BaseReconciliationVo.class, response);
    }

    /**
     * 获取对账管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("order:reconciliation:query")
    @GetMapping("/{id}")
    public R<BaseReconciliationVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable String id) {
        return R.ok(baseReconciliationService.queryById(id));
    }

    /**
     * 新增对账管理
     */
    @SaCheckPermission("order:reconciliation:add")
    @Log(title = "对账管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BaseReconciliationBo bo) {
        return toAjax(baseReconciliationService.insertByBo(bo));
    }

    /**
     * 修改对账管理
     */
    @SaCheckPermission("order:reconciliation:edit")
    @Log(title = "对账管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BaseReconciliationBo bo) {
        return toAjax(baseReconciliationService.updateByBo(bo));
    }

    /**
     * 删除对账管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("order:reconciliation:remove")
    @Log(title = "对账管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] ids) {
        return toAjax(baseReconciliationService.deleteWithValidByIds(List.of(ids), true));
    }
}
